//---------------------------------------------------------------------------
// MyMan Class
// Author : Alex Woo-Hyun Kim at www.webrobot.org
// Email : woorung@webrobot.org,webrobot@hotmail.com,woorung@empal.com
// Date : Apr. 18, 2002
//
// This class executes real SQL query after connecting to MySQL.
//---------------------------------------------------------------------------
#include <stdio.h>
#include <mysql.h>
#include "CMyMan.h"
#include "CMyResultSet.h"
//---------------------------------------------------------------------------
CMyMan::CMyMan(char *pszConfigFileName) : CMyConMan(pszConfigFileName)
{
	m_pMyCon = NULL;
	m_pMyResultSet = NULL;
	m_lMyID = 0;

	//m_pMyCon = GetConnection();
}
//---------------------------------------------------------------------------
CMyMan::~CMyMan()
{
	if(m_pMyCon!=NULL) {
		FreeConnection(m_pMyCon);
		m_pMyCon = NULL;
	}
}
//---------------------------------------------------------------------------
CMyResultSet *CMyMan::GetMyResultSet(CMyStatement *MyStatement)
{
	GetMyResultSet(MyStatement->GetStatement());
}
//---------------------------------------------------------------------------
CMyResultSet *CMyMan::GetMyResultSet(char *pSqlStmt)
{
	m_pMyCon = GetConnection();

/*
	m_pMyCon = mysql_init(NULL);
	mysql_real_connect(m_pMyCon,"localhost","kcube","dkagh","dic",0,NULL,0);
*/

	if(mysql_query(m_pMyCon,pSqlStmt)!=0) {
#ifdef DEBUG
	Debug(pSqlStmt);
	Debugln(" query fails");
#endif
		FreeConnection(m_pMyCon);
		m_pMyCon = NULL;
		return NULL;
	}
#ifdef DEBUG
	Debug(pSqlStmt);
	Debugln(" query success");
#endif

	m_lMyID = mysql_insert_id(m_pMyCon);

	if((m_pMyResultSet=mysql_store_result(m_pMyCon))==NULL) {
		if(mysql_field_count(m_pMyCon)>0) {
			FreeConnection(m_pMyCon);
			m_pMyCon = NULL;
			return NULL;
		}
		else {
			unsigned long ulAffectedRows = mysql_affected_rows(m_pMyCon);
			FreeConnection(m_pMyCon);
			m_pMyCon = NULL;
			return new CMyResultSet(ulAffectedRows);	
		}
	}
#ifdef DEBUG
	Debugln("storing result okay");
#endif
	FreeConnection(m_pMyCon);	
	m_pMyCon = NULL;
#ifdef DEBUG
	Debugln("connection free");
#endif

	return new CMyResultSet(m_pMyResultSet);
}
//---------------------------------------------------------------------------
long CMyMan::GetInsertID()
{
	return m_lMyID;
}
//---------------------------------------------------------------------------
